#include <QDir>
#include <QFile>
#include <QPushButton>
#include <QStringBuilder>
#include "CreateModule.h"
#include "ui_CreateModule.h"

CreateModule::CreateModule(const QString& path, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CreateModule),
    _path(path)
{
    ui->setupUi(this);

    ui->modName->setBasePath(path);
    ui->modName->setDirRequest(true);
    ui->lblPath->setText(_path);
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
    connect(ui->modName, SIGNAL(textChanged(QString)), SLOT(onTextChanged(QString)));
    connect(ui->buttonBox, SIGNAL(accepted()), SLOT(onAccepted()));

}

CreateModule::~CreateModule()
{
    delete ui;
}

void CreateModule::onTextChanged(const QString &)
{
    ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!ui->modName->exists());
}

void CreateModule::onAccepted()
{
    QFileInfo info(ui->modName->fullFileName());
    if (info.absoluteDir().mkdir(info.baseName())){
        QFile file(info.absoluteFilePath() % QDir::separator() % "__init__.py");
        if (file.open(QIODevice::WriteOnly)){
            file.close();
        }
        accept();
    }
}

QStringList CreateModule::openAfter()
{
    return QStringList() << ui->modName->fullFileName() % QDir::separator() % "__init__.py";
}
